﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Media;
using System.Windows.Navigation;


namespace A_larm_Shake
{
    public partial class page_stopwatch : PhoneApplicationPage
    {
        int n = 1;
        Stopwatch stopwatch = new Stopwatch();
        TimeSpan suspensionAdjustment = new TimeSpan();
        string decimalSeparator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;

        public page_stopwatch()
        {
            InitializeComponent();
            DisplayTime();
            bt_Pausewatch.Visibility = Visibility.Collapsed;
            bt_Lapwatch.Visibility = Visibility.Collapsed;
        }

        void OnCompositionTargetRendering(object sender, EventArgs args)
        {
            DisplayTime();
        }

        void DisplayTime()
        {
            TimeSpan elapsedTime = stopwatch.Elapsed + suspensionAdjustment;
            string str = null;

            str = String.Format("{0:D2} : {1:D2} : {2:D2} : {4:D2}",
                                       elapsedTime.Hours, elapsedTime.Minutes,
                                       elapsedTime.Seconds, decimalSeparator,
                                       elapsedTime.Milliseconds / 10);

            txtDigitalClock.Text = str;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs args)
        {
            PhoneApplicationService service = PhoneApplicationService.Current;
            service.State["suspensionAdjustment"] = suspensionAdjustment + stopwatch.Elapsed;
            service.State["tombstoneBeginTime"] = DateTime.Now;

            base.OnNavigatedFrom(args);
        }
        protected override void OnNavigatedTo(NavigationEventArgs args)
        {
            PhoneApplicationService service = PhoneApplicationService.Current;

            if (service.State.ContainsKey("stopWatchRunning"))
            {
                suspensionAdjustment = (TimeSpan)service.State["suspensionAdjustment"];

                if ((bool)service.State["stopWatchRunning"])
                {
                    suspensionAdjustment += DateTime.Now -
                                                (DateTime)service.State["tombstoneBeginTime"];
                }
                else
                {
                    DisplayTime();
                }
            }
            base.OnNavigatedTo(args);
        }
        private void bt_Startwatch_Click(object sender, RoutedEventArgs e)
        {
            bt_Startwatch.Visibility = Visibility.Collapsed;
            bt_Cancelwatch.Visibility = Visibility.Collapsed;
            bt_Pausewatch.Visibility = Visibility.Visible;
            bt_Lapwatch.Visibility = Visibility.Visible;

            stopwatch.Start();
            CompositionTarget.Rendering += OnCompositionTargetRendering;
        }

        private void bt_Pausewatch_Click(object sender, RoutedEventArgs e)
        {
            bt_Startwatch.Visibility = Visibility.Visible;
            bt_Cancelwatch.Visibility = Visibility.Visible;
            bt_Pausewatch.Visibility = Visibility.Collapsed;
            bt_Lapwatch.Visibility = Visibility.Collapsed;

            TimeSpan elapsedTime = stopwatch.Elapsed + suspensionAdjustment;
            string str = null;

            str = String.Format("{0:D2} : {1:D2} : {2:D2} : {4:D2}",
                                       elapsedTime.Hours, elapsedTime.Minutes,
                                       elapsedTime.Seconds, decimalSeparator,
                                       elapsedTime.Milliseconds / 10);
            txtDigitalClock.Text = str;
            stopwatch.Stop();

            CompositionTarget.Rendering -= OnCompositionTargetRendering;

            textBl.Text += "Stop -> " + str + "\n";
        }

        private void bt_Lapwatch_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan elapsedTime = stopwatch.Elapsed - suspensionAdjustment;

            string str = null;
            str = String.Format("{0:D2} : {1:D2} : {2:D2} : {4:D2}",
                                       elapsedTime.Hours, elapsedTime.Minutes,
                                       elapsedTime.Seconds, decimalSeparator,
                                       elapsedTime.Milliseconds / 10);
            

            txtDigitalClock.Text = str;

            textBl.Text += "Lap " + n +" -> "+ str + "\n";
            n++;
        }

        private void bt_Cancelwatch_Click(object sender, RoutedEventArgs e)
        {
            //txtDigitalClock.Text = ("00:00:00:00");
            textBl.Text = ("");
            n = 1;
            stopwatch.Reset();
            suspensionAdjustment = new TimeSpan();
            DisplayTime();
        }
    }
}